<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/18
 * Time: 9:55
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Complain extends Base
{
    /**
     * 任务投诉列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //状态
            if(isset($params['complain_status']) && $params['complain_status'] != ''){
                $where['complain_status'] = $params['complain_status'];
            }
            //手机号
            if(isset($params['user_phone']) && $params['user_phone'] != '')
            {
                $where['user_phone'] = ['like', '%' . $params['user_phone'] . '%'];
            }
            
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('complain')
                ->join('up_u_user', 'user_id = complain_user_id', 'left')
                ->join('up_task', 'task_id = complain_task_id', 'left')
                ->join('up_shops', 's_id = complain_shop_id', 'left')
                ->where($where)
                ->count();
            $lists = Db::name('complain')
                ->join('up_u_user', 'user_id = complain_user_id', 'left')
                ->join('up_task', 'task_id = complain_task_id', 'left')
                ->join('up_shops', 's_id = complain_shop_id', 'left')
                ->where($where)
                ->field('complain_id, complain_time, complain_status, user_phone, task_number, s_name, 
                s_num, s_thumb, complain_content, complain_img')
                ->page($page,$limit)
                ->order('complain_time desc')
                ->select ();
            foreach ($lists as $key => $vo){
                if($vo['complain_img'] != ''){
                    $complain_img = trim($vo['complain_img'],',');
                    $image = explode (',',$complain_img);
                    $lists[$key]['picture'] = $image[0];
                    unset($image[0]);
                    $lists[$key]['complain_img'] = array_values($image);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 投诉详情
     * @return mixed
     */
    public function complainDetail(){
        $complain_id = input('complain_id');
        $info = Db::name('complain')
            ->join('up_u_user', 'user_id = complain_user_id', 'left')
            ->join('up_task', 'task_id = complain_task_id', 'left')
            ->join('up_shops', 's_id = complain_shop_id', 'left')
            ->where('complain_id', $complain_id)
            ->field('complain_id, complain_time, complain_status, user_phone, task_number, s_name, 
                s_num, s_thumb, complain_content, complain_img')
            ->find();
        $info['complain_img'] = explode(',', trim($info['complain_img'], ','));
        if($info['complain_status'] == 1){
            $info['status'] = '待处理';
        }elseif($info['complain_status'] == 2){
            $info['status'] = '已处理';
        }else{
            $info['status'] = '已忽略';
        }
        $this->assign([
            'complain_id' => $complain_id,
            'info' => $info
        ]);
        return $this->fetch('complain/complaindetail');
    }
    
    /**
     * 忽略
     * @return \think\response\Json
     */
    public function complainPast(){
        $complain_id = input('id');
        $res = Db::table('up_complain')->where('complain_id', $complain_id)->setField('complain_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 处理
     * @return \think\response\Json
     */
    public function complainDone(){
        $complain_id = input('id');
        $res = Db::table('up_complain')->where('complain_id', $complain_id)->setField('complain_status', 2);
        $res = Db::table('up_complain')->where('complain_id', $complain_id)->setField('complain_datetime', date('Y-m-d H:i:s'));
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}